package com.tang.websocket.j2ee;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import javax.websocket.server.ServerEndpointConfig;

/**
 * @ClassName: CustomerEndpointConfigurator
 * @Description: 因为spring是单实例，为解决ServerEndpoint注解的websocket无法注入Bean，需要在此处配置，向spring的上下文中注入Bean
 * @date: 2019/7/29
 * @author: lurf@suxiniot.com
 */
public class CustomerEndpointConfigurator extends ServerEndpointConfig.Configurator implements ApplicationContextAware {

    private static volatile BeanFactory context;

    public CustomerEndpointConfigurator() {}

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        CustomerEndpointConfigurator.context = applicationContext;
    }

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        return context.getBean(clazz);
    }
}
